<template>
  <div class="registerbox">
    <el-card class="gray-card no-border no-margin-top"
             shadow="never">
      <el-form ref="form"
               :model="form"
               class="form">
        <el-form-item label="模板编号：">
          <el-input v-model="form.templateNo"></el-input>
        </el-form-item>

        <el-form-item label="消息类型：">
          <el-select v-model="form.type"
                     placeholder="请选择消息类型"
                     class="">
            <el-option v-for="item in type"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="消息内容：">
          <el-input type="textarea"
                    class="textarea"
                    v-model="form.template"></el-input>
        </el-form-item>

        <el-form-item label="模板用途：">

          <el-radio-group v-model="form.useType">
            <el-radio :label="1">发送</el-radio>
            <el-radio :label="2">接收</el-radio>
            <el-radio :label="3">展现</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="details-foot">
      <div class="cell-btn">
        <el-button type="primary"
                   @click.native="handleSubmit">确定</el-button>
        <el-button @click.native="handleClose">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  addNotifyTemplate,
  updateNotifyTemplate,
} from '@/api/business/notifyTemplate'

export default {
  name: 'addEdit',
  props: {
    isVisibleAdd: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      title: '新增通知',
      type: [
        {
          value: '1',
          label: '检验消息',
        },
        {
          value: '2',
          label: '耗材消息',
        },
        {
          value: '3',
          label: '质控消息',
        },
        {
          value: '4',
          label: '培训消息',
        },
        {
          value: '5',
          label: '仪器消息',
        },
        {
          value: '6',
          label: '其他消息',
        },
      ],
      form: {
        id: '',
        templateNo: '',
        template: '',
        type: '',
        useType: '',
        dbStatus: 1,
      },
      editId: '',
    }
  },
  methods: {
    handleSubmit() {
      if (this.editId === '') {
        addNotifyTemplate(this.form).then((res) => {
          this.handleClose()
        })
      } else {
        updateNotifyTemplate(this.form).then((res) => {
          this.handleClose()
        })
      }
    },
    handleOpenEdit(obj) {
      this.editId = obj.id
      this.form.id = obj.id
      this.form.templateNo = obj.templateNo
      this.form.type = obj.type
      this.form.template = obj.template
      this.form.useType = obj.useType
    },
    handleOpenAdd() {
      this.editId = ''
      this.form.id = ''
      this.form.templateNo = ''
      this.form.type = ''
      this.form.template = ''
      this.form.useType = ''
      this.form.dbStatus = 1
    },
    handleClose() {
      this.$emit('close', false)
    },
  },
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}
</style>
